#include <iostream>
#include <opencv2/opencv.hpp>
#include <io.h>
#include <opencv2/core/utils/logger.hpp>
#include <ctime>

#include "onnx_run.h"


int main(int argc, char *argv[]) {
    //LOG_LEVEL_ERROR - 只输出错误日志 / LOG_LEVEL_SILENT - 不在输出日志
    cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);

    // 图像全路径单斜杠需要转义，或者需要R"()"这样
    // 模型路径
    std::string onnx_path = R"(D:\my_program\study-c\bushu\yolo-onnxruntime-test\yolo_onnxruntime\best_20221122.onnx)";
    // 框和iou置信度
    const float conf_threshold = 0.3f;
    const float iou_threshold = 0.45f;
    // 检测类别
    const std::vector<std::string> class_names = {"ng"};
    // 是否画框
    const bool draw_box = true;

    cv::VideoCapture cap;
    try {
        // 调用构造函数，初始化模型
        YoloDetect yolo_detect(onnx_path, conf_threshold);
        std::cout << "Model was initialized." << std::endl;

        std::string video_path = R"(C:\\Users\\ZHONGBO\\Downloads\\Video\\car.mp4)";
//        cap = cv::VideoCapture(0);
        cap = cv::VideoCapture(video_path);
//        image = cv::imread(imagePath);

        // 存储画框帧
        cv::Mat frame;
        // 存储输出结果
        std::vector<Detection> result;

        double start, end, t;
        while (cap.isOpened()) {

            start = clock();

            cap >> frame;  // 把cap中的内容赋值给frame
            // 侦测
            result = yolo_detect.detect(frame);
            DrawImg(frame,result,class_names);

            end = clock();
            // 计算毫秒ms
            t = (end - start) / CLOCKS_PER_SEC * 1000;
            std::cout << t << std::endl;
            double fps = 1000 / t;

            cv::putText(frame, std::to_string(fps), cv::Point(20, 50), cv::FONT_HERSHEY_COMPLEX, 1,
                        cv::Scalar(255, 0, 0), 2, cv::LINE_AA);
            cv::imshow("result", frame);
            // cv::imwrite("result.jpg", image);
            cv::waitKey(1);

        }

    }
    catch (const std::exception &e) {
        std::cerr << e.what() << std::endl;
        return -1;
    }

    cap.release();
    cv::destroyAllWindows();

    return 0;
}
//E:\workspace\clion\yolo-onnxruntime\cmake-build-debug>yolo_onnxruntime.exe --model_path=../models/yolov5s.onnx --image=bus.jpg --class_names=../models/coco.names
